*********************************************************
*														*
*	Land Titles, Race, and Political Violence       	*
*														*
*********************************************************

clear all
set more off
pause on
set trace off
cap log close
set matsize 5000


//Install Programs

foreach var in  margeff estout avg_effect sutex parmest binscatter ranktest ivreg2 cmp rd coefplot {

	cap which `var'      // check whether -ado- is installed

	qui if _rc ssc install `var', replace // and get it if not
}


	loc controls  altura rainfall coca_0_1 presence_mines1560 num_encomiendas1560 royalroaddist distancia_mercado pop95 pct_minority85 conflictos_1901_1917 conflictos_1918_1931 anucraids1971_78 totviolencia85 spatlagguerrsum100_100_1995 ltotal_plots_rfmd_1960_85
	  
	
	loc region_timetrend  gandina##c.Año gcaribe##c.Año gpacifica##c.Año gorinoquia##c.Año gamazonia##c.Año 
	
	loc region_fe  i.gandina*i.Año i.gcaribe*i.Año i.gpacifica*i.Año i.gorinoquia*i.Año i.gamazonia*i.Año  
	
//.  Baseline Specification Checks

		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear
	
	gen post=(Año>1995)
	gen rany_titleXpost=any_title*post
	egen total_violence=rowtotal( sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)	
	gen attacks_pc=total_violence/pop95
	gen ln_attacks=ln(total_violence + 0.01)
	gen ln_attacks_pc=ln(attacks_pc + 0.01)
	
	loc controls_spa altura areaoficialkm2 ocup_espan discapital dismdo  conflicto conflictos_1901_1917  conflictos_1918_1931
	
	** Controls X post
	loc controlsXpost
	loc i
	
	foreach control of loc controls {
	
		loc i c.`control'##post
		
		loc controlsXpost `controlsXpost' `i' 

	}
	
	di in red "`controlsXpost'"

	
	** Controls X year
	loc controlsXyear
	loc x
	
	foreach control of loc controls {
	
		loc x c.`control'##c.Año
		
		loc controlsXyear `controlsXyear' `x' 

	}
	
	
	di in red "`controlsXyear'"
	
		
	foreach x in 1995 {
	
		preserve
		
			keep if Año<=`x'
						
			collapse (mean)  spatlagguerrsum100_100, by(codmpio)
			
			ren (spatlagguerrsum100_100)=_`x'
			
			tempfile neighbor
			save `neighbor', replace
		
		
		restore
		
		merge m:1 codmpio using `neighbor'
		drop _m
	
	
	}
* Clear any existing stored estimates
estimates clear

		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear

	gen post=(Año>1995)
	gen any_titleXpost=any_title*post
	gen any_title2=any_title
	gen post2=post
	egen total_violence=rowtotal( sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)

	** Controls X post
	loc controlsXpost
	loc i
	
	foreach control of loc controls {
	
		loc i c.`control'##post
		
		loc controlsXpost `controlsXpost' `i' 

	}
	
	** Controls X year
	loc controlsXyear
	loc x
	
	foreach control of loc controls {
	
		loc x c.`control'##c.Año
		
		loc controlsXyear `controlsXyear' `x' 

	}
	
	di in red "`controlsXyear'"
	
		
	gen as_total_violence=asinh(total_violence)
	
	preserve
	
		keep if Año==1993
		
		collapse (mean) gini, by(codmpio)
		ren gini gini93
		
		tempfile gini
		save `gini', replace
	
	
	restore
	
	
	merge m:1 codmpio using `gini'
	drop _m
	
	order gini93, after(landgini02)
	
		
	foreach x in 1995 {
	
		preserve
		
			keep if Año<=`x'
						
			collapse (mean)  spatlagguerrsum100_100, by(codmpio)
			
			ren (spatlagguerrsum100_100)=_`x'
			
			tempfile neighbor
			save `neighbor', replace
		
		
		restore
		
		merge m:1 codmpio using `neighbor'
		drop _m
	
	
	}
	
	foreach x in 2000 {
	
		preserve
		
			keep if Año<=`x'
						
			collapse (mean)  avaluo_rur_pcrur, by(codmpio)
			
			ren (avaluo_rur_pcrur)=_`x'
			
			tempfile value
			save `value', replace
		
		
		restore
		
		merge m:1 codmpio using `value'
		drop _m
	
	
	}
	
	estimates clear

	loc indepvars
	loc allvars
		
	foreach y of varlist total_violence sumattacks_public sumattacks_paramilitary sumattacks_guerrilla {
	
		cap gen as_`y'=asinh(`y')

		preserve
		
			 eststo : xi: areg  as_`y'  c.avaluo_rur_pcrur_2000##post i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
					
			
			estadd 	 loc time_FE "Yes"
			estadd 	 loc loc_FE "Yes"	
			estadd 	 loc timetrends "No"	
			estadd 	 loc controls "Yes"	
			estadd 	 loc controlsXpost "Yes"	
			estadd 	 loc DepartmentXyear "Yes"
			estadd	 loc region_fe "No"
			estadd 	 loc cluster = e(N_clust) 	

			sum as_`y'    if  e(sample)==1
			estadd scalar depvarmean=r(mean)
				
				
			 eststo : xi: areg  as_`y'  c.avaluo_rur_pcrur_2000##post any_title##post i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
					
			estadd 	 loc time_FE "Yes"
			estadd 	 loc loc_FE "Yes"	
			estadd 	 loc timetrends "No"	
			estadd 	 loc controls "Yes"	
			estadd 	 loc controlsXpost "Yes"	
			estadd 	 loc DepartmentXyear "Yes"
			estadd	 loc region_fe "No"
			estadd 	 loc cluster = e(N_clust)  	
			
			sum as_`y'    if  e(sample)==1
			estadd scalar depvarmean=r(mean)
				
			eststo : xi: areg  as_`y' any_title##c.avaluo_rur_pcrur_2000##post  i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
					
			estadd 	 loc time_FE "Yes"
			estadd 	 loc loc_FE "Yes"	
			estadd 	 loc timetrends "No"	
			estadd 	 loc controls "Yes"	
			estadd 	 loc controlsXpost "Yes"	
			estadd 	 loc DepartmentXyear "Yes"
			estadd	 loc region_fe "No"
			estadd 	 loc cluster = e(N_clust) 	
			
			sum as_`y'    if  e(sample)==1
			estadd scalar depvarmean=r(mean)
				
		restore 
					
	}
	
		
	#d ;
		esttab using "TableF4_any_title.tex", keep(1.any_title#1.post 1.post#c.avaluo_rur_pcrur_2000 1.any_title#1.post#c.avaluo_rur_pcrur_2000) order(  1.post#c.avaluo_rur_pcrur_2000 1.any_title#1.post 1.any_title#1.post#c.avaluo_rur_pcrur_2000) replace se b(%9.3f) se(%9.3f) ar2(%9.3f) ar2  eqlabels(none) f  nodep nobaselevels  nonum noobs  label gaps nolines collabels(, none) star(* 0.10 ** 0.05 *** 0.01) 
		varlabels(1.any_title#1.post				"Law 70 title (0/1) $\times$ Post (>1995)"
				 1.post#c.avaluo_rur_pcrur_2000 	"Land Value (2000) $\times$ Post (>1995)" 	
				 1.any_title#1.post#c.avaluo_rur_pcrur_2000 "Any title $\times$ Land Value (2000)  $\times$ Post (>1995)")
		refcat(1.post#c.avaluo_rur_pcrur_2000 "\hline \\ \textbf{Panel A: Land Value}" 
			   , nolabel)
		mgroups("\specialcell{All \\ Attacks}" "\specialcell{Police \& \\ Army Attacks}" "\specialcell{Paramilitary \\ Attacks}" "\specialcell{Guerrilla \\ Attacks}", pattern(1 0 0 1 0 0 1 0 0 1 0 0)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  
		nonotes stats(r2_a N cluster    depvarmean, layout(@ @ @ @ ) 
		fmt(a3 a3 a3 a3 a3 ) labels("Adj. R-squared" "Observations" "Clusters \smallskip" "Dependent Variable Mean"   ));
												   
	#d cr
